package com.boco.modules.fdoc.service.impl.surrender;


import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.boco.modules.fdoc.dao.ExpertUserSignDao;
import com.boco.modules.fdoc.dao.sign.DiseaseLabelDao;
import com.boco.modules.fdoc.dao.sign.PersonDeseaseInfoDao;
import com.boco.modules.fdoc.dao.sign.PersonInformationDao;
import com.boco.modules.fdoc.dao.sign.SendMsgDao;
import com.boco.modules.fdoc.dao.sign.SignDao;
import com.boco.modules.fdoc.dao.sign.UserDao;
import com.boco.modules.fdoc.dao.surrender.SurrenderInformationDao;
import com.boco.modules.fdoc.dao.surrender.SurrenderRequestDao;
import com.boco.modules.fdoc.model.DiseaseLabelPersonRelationEntity;
import com.boco.modules.fdoc.model.PersonDeseaseInfoEntity;
import com.boco.modules.fdoc.model.SignAgreementEntity;
import com.boco.modules.fdoc.model.UserEntity;
import com.boco.modules.fdoc.model.expert.ExpertUserSignEntity;
import com.boco.modules.fdoc.model.surrender.SurrenderInformationEntity;
import com.boco.modules.fdoc.model.surrender.SurrenderRequestEntity;
import com.boco.modules.fdoc.service.surrender.SurrenderRequestService;
import com.boco.modules.fdoc.vo.surrender.SignInfoVo;
import com.boco.modules.fdoc.vo.surrender.SurrenderRequestVo;

/**
 * Description
 * @author lzz
 * @date 2017年11月29日 上午10:28:12
 */
@Service
public class SurrenderRequestServiceImpl implements SurrenderRequestService{
	
	@Resource
	SurrenderRequestDao requestDao;
	@Resource
	SurrenderInformationDao inforDao;
	@Resource
	SignDao signDao;
	@Resource
	PersonInformationDao personDao;
	@Resource
	PersonDeseaseInfoDao deseaseInfoDao;
	@Resource
	DiseaseLabelDao diseaselabelDao;
    @Resource
    UserDao userDao;
    @Resource
    SendMsgDao sendMsgDao;
    @Resource
    ExpertUserSignDao expertSignDao;

	@Override
	public Integer getRequestCount(SurrenderRequestVo searchVo) {
		return requestDao.getRequestCount(searchVo);
	}

	@Override
	public List<SurrenderRequestVo> getRequestList(SurrenderRequestVo searchVo) {
		return requestDao.getRequestList(searchVo);
	}

	@Override
	public Integer getMaxReasonValue() {
		return requestDao.getMaxReasonValue();
	}

	@Override
	public String getReasonByValue(Integer a) {
		return requestDao.getReasonByValue(a);
	}

	@Override
	public SurrenderRequestEntity getRequestById(Integer id) {
		return requestDao.getRequestById(id);
	}


	@Override
	public int refuseSurrender(SurrenderRequestEntity requestEntity) {
		return requestDao.updateSurrenderById(requestEntity);
	}

	@Override
	@Transactional
	public int agreeSurrender(SurrenderRequestEntity requestEntity) {
		
		
		Integer requestId=requestEntity.getId();
		String personId=requestEntity.getPersonId();
		SurrenderInformationEntity insertEntity=inforDao.getSignInfo(personId);
		
		//获取专家信息
		ExpertUserSignEntity expertUserSignEntity=expertSignDao.getExpertSignByPersonId(personId);
		if(expertUserSignEntity==null){
			insertEntity.setExpertId("-1");
		}else{
			insertEntity.setExpertId(expertUserSignEntity.getExpertId());
		}
		
		//将之前的签约数据存入解约信息表内
		insertEntity.setRequestId(requestId);
		inforDao.insert(insertEntity);
		
		//取消关注专家，既删除专家关注表中信息
		if(expertUserSignEntity!=null){
			expertSignDao.cancleExpertSign(personId);
		}
		
		//将解约请求表中审核状态变更
		requestDao.updateSurrenderById(requestEntity);
		
		//修改签约标识
		personDao.updateSign(personId);
		//删除协议表信息（使用老版本的残留数据）
		Integer signId=signDao.getSignIdByPersonId(personId);
		SignAgreementEntity agreementEntity=signDao.getAgreementBySignId(signId);
		if(agreementEntity != null){
			signDao.deleteAgreement(signId);
		}
		//删除签约信息
		signDao.deleteSignByPersonId(personId);
		//删除签约为居民打上的标签
		PersonDeseaseInfoEntity deseaseInfoEntity=deseaseInfoDao.get(personId);
		if(deseaseInfoEntity != null){
			deseaseInfoDao.deleteByPersonId(personId);
		}
		//删除签约后居民的归类
		List<DiseaseLabelPersonRelationEntity> relationEntities=diseaselabelDao.getRelationByPersonId(personId);
		if(relationEntities != null){
			diseaselabelDao.deleteByPersonId(personId);
		}
		//删除消息推送表钟信息
		UserEntity userEntity=userDao.getUserByPersonId(personId);
		if(userEntity != null){
			sendMsgDao.deleteByPersonId(userEntity.getUid());
		}
		
		return 1;
	}

	@Override
	public SignInfoVo getSurrenderedById(Integer id) {
		return requestDao.getSurrenderedById(id);
	}

	@Override
	public SignInfoVo getOtherById(Integer id) {
		return requestDao.getOtherById(id);
	}

	@Override
	public Integer getSignIdByPersonId(String personId) {
		return signDao.getSignIdByPersonId(personId);
	}

	

	

	

}
